import { FormSchema } from '@/components/Form'
import { BasicColumn } from '@/components/Table'
import { DescItem } from '@/components/Description'
import { dicDictList } from '@service/sys/dict'
import { dictConversion } from '@admin/utils'
import { DicSortEnum } from '@enums/basic'
import { RoleIM } from '@type/system'

/** 字典查询 */
export const dictMap = await dicDictList([
  'sys_data_scope',
  'sys_normal_disable',
])

/** 字典表 */
export const dict: any = {
  DataScopeOptions: dictMap['sys_data_scope'],
  NormalDisableOptions: dictMap['sys_normal_disable'],
}

/** 表格数据 */
export const columns: BasicColumn[] = [
  {
    title: '角色编码',
    dataIndex: 'code',
    width: 220,
  },
  {
    title: '角色名称',
    dataIndex: 'name',
    width: 220,
  },
  {
    title: '角色权限字符串',
    dataIndex: 'roleKey',
    width: 220,
  },
  {
    title: '数据范围',
    dataIndex: 'dataScope',
    width: 220,
    customRender: ({ record }) => {
      const data = record as RoleIM
      return dictConversion(dict.DataScopeOptions, data.dataScope)
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 220,
    customRender: ({ record }) => {
      const data = record as RoleIM
      return dictConversion(dict.NormalDisableOptions, data.status)
    },
  },
]

/** 查询数据 */
export const searchFormSchema: FormSchema[] = [
  {
    label: '角色编码',
    field: 'code',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '角色名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '角色权限字符串',
    field: 'roleKey',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '数据范围',
    field: 'dataScope',
    component: 'Select',
    componentProps: {
      options: dict.DataScopeOptions,
    },
    colProps: { span: 6 },
  },
  {
    label: '状态',
    field: 'status',
    component: 'Select',
    componentProps: {
      options: dict.NormalDisableOptions,
    },
    colProps: { span: 6 },
  },
]

/** 表单数据 */
export const formSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '角色编码',
    field: 'code',
    component: 'Input',
  },
  {
    label: '角色名称',
    field: 'name',
    component: 'Input',
  },
  {
    label: '角色权限字符串',
    field: 'roleKey',
    component: 'Input',
  },
  {
    label: '数据范围',
    field: 'dataScope',
    component: 'Select',
    componentProps: {
      options: dict.DataScopeOptions,
      showSearch: true,
      optionFilterProp: 'label',
    },
  },
  {
    label: '显示顺序',
    field: 'sort',
    component: 'InputNumber',
    defaultValue: DicSortEnum.ZERO,
  },
  {
    label: '状态',
    field: 'status',
    component: 'RadioButtonGroup',
    componentProps: {
      options: dict.NormalDisableOptions,
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
]

/** 详情数据 */
export const detailSchema: DescItem[] = [
  {
    label: '角色编码',
    field: 'code',
  },
  {
    label: '角色名称',
    field: 'name',
  },
  {
    label: '角色权限字符串',
    field: 'roleKey',
  },
  {
    label: '数据范围',
    field: 'dataScope',
    render: (val) => {
      return dictConversion(dict.DataScopeOptions, val)
    },
  },
  {
    label: '显示顺序',
    field: 'sort',
  },
  {
    label: '状态',
    field: 'status',
    render: (val) => {
      return dictConversion(dict.NormalDisableOptions, val)
    },
  },
  {
    label: '备注',
    field: 'remark',
  },
]
